home *** CD-ROM | disk | FTP | other *** search
-
- //
- // comma_long function: convert long to ASCII,
- // with commas every three digits and sign preservation:
- //
-
- #include <stdio.h>
- #include <math.h> // needed for labs()
-
- void comma_long(long number, char *string);
-
- void main() // test calls to comma_long() function
- {
- char string[100];
-
- comma_long(123456L, string);
- printf("result 1: %s\n", string);
-
- comma_long(-2323435L, string);
- printf("result 2: %s\n", string);
- }
-
- void comma_long(long number, char *string)
- {
- if (labs(number) >= 1000) {
- comma_long(number/1000, string);
- sprintf(string + strlen(string), ",%03ld",
- labs(number) % 1000);
- }
- else
- sprintf(string, "%ld", number);
- }
-